home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5631 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.2 KB

  1. Message-ID: <311693FD.6BBF@novell.com>
  2. Date: Mon, 05 Feb 1996 16:34:21 -0700
  3. From: Sukanta Ganguly <sukanta_ganguly@novell.com>
  4. Organization: Novell Inc
  5. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  6. MIME-Version: 1.0
  7. Newsgroups: comp.lang.c++
  8. Subject: Re: Question on constructors
  9. References: <3103E642.41C6@bme.jhu.edu>
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. NNTP-Posting-Host: sganguly.npd.provo.novell.com
  13. Path: news.provo.novell.com!
  14.  
  15. Stephanos Androutsellis-Theotokis wrote:
  16. > I have the following question on constructors:
  17. > I wrote a c++ library that uses a dummy non-local static instance of a class
  18. > with a default constructor to force library initialization.
  19. > I thought C++ semantics guaranteed that the constructor for this dummy
  20. > object is called before any other functions in the same "translation unit",
  21. > or, at the very least, before any member functions of this dummy object
  22. > are called.
  23. > Is this assumption wrong?
  24. > If so, how can one force a certain constructor to be called before any
  25. > others?
  26. > If not, why does the following program violate this assumption under my
  27. > Watcom v10.0 compiler?
  28. > File z.cpp:
  29. > ////////////////////////////////////////////////////////////////
  30. ////////////////
  31. > #include "mylib.h"
  32. > #include <iostream.h>
  33. > #include <string.h>
  34. > class A {
  35. >    public:
  36. >         A();
  37. >         A(char *as);
  38. >         ~A();
  39. >    private:
  40. >         char *s;
  41. > };
  42. > A aa("main global non static");
  43. > static A a("main global static");
  44. > A::A(char *as)
  45. > {
  46. >         cout << "A constructor called for " << as << "\n";
  47. >         s = strdup(mylibf1(as));
  48. >         cout << "S is now" << s << "\n";
  49. > }
  50. > A::~A()
  51. > {
  52. >         cout << "A destructor called\n";
  53. > }
  54. > main() {
  55. >         A c("main local non static");
  56. >         static A d("main local static");
  57. > }
  58. > ////////////////////////////////////////////////////////////////
  59. ////////////////
  60. > File mylib.h
  61. > char *
  62. > mylibf1(char *as);
  63. > ////////////////////////////////////////////////////////////////
  64. ////////////////
  65. > File mylib.cpp
  66. > #include "mylib.h"
  67. > #include <string.h>
  68. > #include <iostream.h>
  69. > #include <malloc.h>
  70. > class B {
  71. >    public:
  72. >         B();
  73. >         ~B();
  74. >         char *libf1(char *as);
  75. >    private:
  76. >         char *s;
  77. > };
  78. > static B dummy;
  79. > B::B()
  80. > {
  81. >    cout << "Constructor for dummy called.\n";
  82. >    s = strdup("Initial str");
  83. > }
  84. > B::~B()
  85. > {
  86. >    cout << "Destructor for dummy called.\n";
  87. >    free(s);
  88. > }
  89. > char *
  90. > mylibf1(char *as)
  91. > {
  92. >    return ::dummy.libf1(as);
  93. > }
  94. > char *
  95. > B::libf1(char *as)
  96. > {
  97. >    char *tmp;
  98. >    tmp = s;
  99. >    s = strdup(as);
  100. >    return tmp;
  101. > }
  102. > ////////////////////////////////////////////////////////////////
  103. ////////////////
  104. > The program crashes because the constructor for dummy is called after the
  105. > constructors for class A objects in file z.cpp,although these objects'
  106. > constructors call (indirectly) member functions of "dummy".
  107. > Note that if the programs are linked the other way round (e.g. changing
  108. > z.cpp to a.cpp) causes the constructors to be executed in the right
  109. > order.
  110. > The question of how to cause a constructor to be called before anything
  111. > else (in any translation unit) also interests me because constructor calling
  112. > sequence determines the sequence in which destructors will be called. In the
  113. > above example, I would like the constructor for "dummy" to be called first
  114. > regardless of whether the constructor for class A objects uses "dummy" members
  115. > or not.  The reason for this is that the destructor for A might use "dummy"
  116. > members, hence requiring "dummy" destructor to be called last.
  117. > Any help would be appreciated.
  118. > Stephanos.
  119. > stheotok@bme.jhu.edu
  120.  
  121. Hi,
  122.  I read your comment on the problem. I would not write it in 
  123. that manner. However, I feel you should create the static dummy 
  124. object within the mylibf1 function.
  125.  
  126. // Try this out, it might solve your problem
  127.  
  128.  
  129.  B::B()
  130.  {
  131.     cout << "Constructor for dummy called.\n";
  132.     s = strdup("Initial str");
  133.  }
  134.  
  135.  B::~B()
  136.  {
  137.     cout << "Destructor for dummy called.\n";
  138.     free(s);
  139.  }
  140.  
  141.  char *
  142.  mylibf1(char *as)
  143.  {
  144.     // This is important. Create the static dummy derived         
  145. // from B within the function itself.
  146.      static B dummy;
  147.  
  148.     return ::dummy.libf1(as);
  149.  }
  150.